<?php

namespace App;

use App\Models\AdminUser;
use App\Models\Department;

class PatentApply extends BaseModel
{
    protected $guarded = ['id', 'created_at', 'updated_at'];
    public static $disk = 'admin';

    protected $casts = [
        'field_data'  => 'json',
        'field_data1' => 'json',
        'field_data2' => 'json',
        'field_data3' => 'json',
        'field_data4' => 'json',
    ];

    const SALT = 'table212';

    const APPLY_CHANNEL = [
        1 => '巴黎公约',
        2 => 'PCT途径'
    ];
    const PATENT_ATTR = [
        1 => '产品及方法专利',
        2 => '产品专利',
        3 => '技术工艺专利',
        4 => '方法专利',
        5 => '其他',
    ];
    const TE_SOURCE = [
        1 => '国家课题',
        2 => '省级课题',
        3 => '厅级课题',
        4 => '横向课题',
        5 => '自选',
    ];
    //te_field
    const TE_FIELD = [
        1  => '高端装备制造',
        2  => '新型材料',
        3  => '新一代信息技术',
        4  => '新能源',
        5  => '现代农业',
        6  => '食品工业',
        7  => '生物医药及健康',
        8  => '节能环保',
        9  => '公共安全和应急处置',
        10 => '土木建筑',
        11 => '其他'
    ];
    //te_maturity
    const TE_MATURITY = [
        1 => '研制',
        2 => '已有样品',
        3 => '通过中试',
        4 => '试生产',
        5 => '批量生产',
        6 => '其他'
    ];
    //te_innovate
    const TE_INNOVATE = [
        1 => '国内先进',
        2 => '国内领先',
        3 => '国际先进',
        4 => '国际领先 ',
    ];

    //ptm
    const PTM = [
        1 => '自行实施',
        2 => '转让',
        3 => '许可',
        4 => '作价入股',
        5 => '质押融资',
        6 => '其他',
    ];
    //novelty
    const NOVELTY = [
        1 => '具有',
        2 => '具有但不明显',
        3 => '不具有',
    ];
    //creativity
    const CREATIVITY = [
        1 => '具有',
        2 => '具有但不明显',
        3 => '不具有',
    ];
    //practicality
    const PRACTICALITY = [
        1 => '能够制造或使用，并产生积极效果',
        2 => '能够制造或使用，但效果一般',
        3 => '能够制造或使用，效果较差 ',
        4 => '不能制造或使用',
    ];
    //prospect
    const PROSPECT = [
        1 => '非常好',
        2 => '较好',
        3 => '一般',
        4 => '不清晰',
    ];

    public function user()
    {
        return $this->belongsTo(AdminUser::class, 'user_id');
    }

    public function department()
    {
        return $this->belongsTo(Department::class, 'department_id');
    }

    public function agency()
    {
        return $this->belongsTo(Agency::class, 'agency_id');
    }

    public function expert()
    {
        return $this->belongsTo(Expert::class, 'expert_id');
    }

    public function experts()
    {
        return $this->belongsToMany(Expert::class, 'patent_assesses', 'patent_apply_id', 'expert_id')
            ->wherePivotNull('deleted_at');
    }

    public function assesses()
    {
        return $this->hasMany(PatentAssess::class, 'patent_apply_id');
    }

    public function patent()
    {
        return $this->hasOne(Patent::class, 'patent_apply_id');
    }

    public function remarks()
    {
        return $this->hasMany(PatentApplyRemark::class, 'patent_apply_id');
    }

    public function transaction()
    {
        return $this->hasOne(Transaction::class, 'patent_sn', 'patent_sn');
    }

    public function syncScore()
    {
        $this->score = $this->assesses()->where('state', 1)->avg('score') ?: 0;
        $this->save();
    }

    public function addSale(array $data)
    {
        $patent_sn = $this->patent_sn;
        $this->transaction()->updateOrCreate(['patent_sn' => $patent_sn], [
            'user_id'        => $this->user_id,
            'department_id'  => $this->department_id,
            'patent_name'    => $this->patent_name,
            'patent_sn'      => $patent_sn,
            'patent_type_id' => $this->patent_type_id,
            'buyer'          => data_get($data, 'buyer'),
            'amount'         => data_get($data, 'amount'),
            'type'           => data_get($data, 'type'),
            'date'           => data_get($data, 'date'),
            'contact_person' => data_get($data, 'contact_person'),
            'contact_mobile' => data_get($data, 'contact_mobile'),
        ]);
        $this->status = 2;
        $this->save();
        $this->patent()->update(['state' => 2]);
    }

}
